/*
 * Copyright (c) 2010 Network Security Lab (NSL), Department of Electrical
 *                    Engineering, University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: He Wu mdzz@uw.edu
 */

/**
 * @file localization.h
 * @brief Localization implementation.
 *
 * Including localization algorithm implementation for locationing nodes and
 * localization packet handler.
 */

#ifndef LOCALIZATION_H
#define LOCALIZATION_H

#define MAX_ANCHORS 20      //!< maximum # of anchors each locator can hear
#define MAX_RSSI_BEACON 128 //!< maximum RSSI recordings in the struct beacon
#define MAX_WAIT 10         //!< report anchor every MAX_WAIT * local_timer_interval
#define LOG10 2.3026        //!< log_10(2)
#define GRID_SIZE 2         //!< grid size for Grid Score
#define GRID_STEP 0.1       //!< grid step size for Grid Score
#define GRID_ERROR_THRESHOLD 0.5  //!< error threshold for Grid Score
#define ALPHA 2.7           //!<
#define K 0.00000025        //!<
#define SIGDB 4             //!<
#define DELTA 0.15          //!<

// default values
#define ANCHOR_INTERVAL 3     //!< anchor broadcast interval
#define LOCATOR_INTERVAL 1.2  //!< locator update interval
#define MAX_RSSI_SAMPLE 6     //!< must be less then MAX_RSSI_BEACON
#define ANCHOR_AGE_LIMIT 10   //!< age limit for RSSI measurement from anchors

// MISC
#define US_TO_SEC 1000000
//#define ENABLE_ANCHOR_LOCATION_REPORT
//#define ENABLE_PROPAGATION

/******************************************************************************/

/**
 * List element of beacons can be heard from current node.
 */
typedef struct beacon_st
{
  __u16 addr; //!< beacon address
  float x;    //!< beacon X coordinate
  float y;    //!< beacon Y coordinate
  __s8 RSSI[MAX_RSSI_BEACON]; //!< List of RSSI measurements
  int RSSIindex;  //!< index to RSSI list
  int age;    //!< gets older if we don't hear from the anchor
  float d;    //!< distance to anchor
} beacon;

/**
 * Grid definition for Grid Score algorithm.
 */
typedef struct grid_st
{
  float xmin; //!< minimum X coordinate
  float xmax; //!< maximum X coordinate
  float ymin; //!< minimum Y coordinate
  float ymax; //!< maximum Y coordinate
  float stepp;  //!< step size for Grid Score
} grid;

/**
 * Path loss model parameters.
 */
typedef struct pathLossModel_st
{
  float alpha;  //!<
  float k;      //!<
  float sigdB;  //!<
} pathLossModel;

/******************************************************************************/

/**
 * @brief Process received localization packet.
 * @param in_pkt Pointer to received localization packet.
 */
void localization_msg(packet* in_pkt);

/**
 * @brief Timer thread for localization algorithms.
 *
 * This timer thread wakes up periodically to perform localization algorithm or
 * to broadcast its location as an anchor.
 */
void *local_timer();

/**
 * @brief Converts RSSI from cc2420 to distance.
 * @param RSSI Packet RSSI measurements from cc2420.
 * @param k
 * @param alpha
 * @return Distance to sender of packet.
 */
float RSSI_to_distance(float RSSI, float k, float alpha);

// localization algorithms

/**
 * @brief Centroid algorithm
 * @param b List of RSSI measurements from anchor nodes.
 * @param num_b Number of anchor nodes current node can hear.
 * @param estX Pointer to estimated X location variable.
 * @param estY Pointer to estimated Y location variable.
 */
void centroid(beacon b[MAX_ANCHORS], int num_b, float *estX, float *estY);

/**
 * @brief Weighted Centroid algorithm
 * @param b List of RSSI measurements from anchor nodes.
 * @param num_b Number of anchor nodes current node can hear.
 * @param estX Pointer to estimated X location variable.
 * @param estY Pointer to estimated Y location variable.
 */
void weightedCentroid(beacon b[MAX_ANCHORS], int num_b, float *estX, float *estY);

/**
 * @brief Grid Score (Voting) algorithm.
 * @param b List of RSSI measurements from anchor nodes.
 * @param num_b Number of anchor nodes current node can hear.
 * @param g Grid definition.
 * @param errorThreshold Error threshold.
 * @param estX Pointer to estimated X location variable.
 * @param estY Pointer to estimated Y location variable.
 */
void voting(beacon b[MAX_ANCHORS], int num_b, grid g, float errorThreshold, float *estX, float *estY);

/**
 * @brief Weighted Grid Score (Voting) algorithm.
 * @param b List of RSSI measurements from anchor nodes.
 * @param num_b Number of anchor nodes current node can hear.
 * @param g Grid definition.
 * @param pl Path loss model parameters.
 * @param estX Pointer to estimated X location variable.
 * @param estY Pointer to estimated Y location variable.
 */
void w_voting(beacon b[MAX_ANCHORS], int num_b, grid g, pathLossModel pl, float *estX, float *estY);

/**
 * @brief Gradient Descent algorithm.
 * @param b List of RSSI measurements from anchor nodes.
 * @param num_b Number of anchor nodes current node can hear.
 * @param delta
 * @param estX Pointer to estimated X location variable.
 * @param estY Pointer to estimated Y location variable.
 */
void gradient(beacon b[MAX_ANCHORS], int num_b, float delta, float *estX, float *estY);

/**
 * @brief Weighted Gradient Descent algorithm.
 * @param b List of RSSI measurements from anchor nodes.
 * @param num_b Number of anchor nodes current node can hear.
 * @param delta
 * @param estX Pointer to estimated X location variable.
 * @param estY Pointer to estimated Y location variable.
 */
void w_gradient(beacon b[MAX_ANCHORS], int num_b, float delta, float * estX, float * estY);

#endif
